package GUI;

/* This class will manage the appearence of the screens */



import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import java.util.Map.Entry;

import java.util.Iterator;


import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.plaf.DimensionUIResource;

import org.jfree.ui.RefineryUtilities;

import org.jfree.ui.RefineryUtilities;

import org.jfree.chart.demo.XYSeriesDemo;
import org.jfree.ui.RefineryUtilities;

import Chat.ClientConsole;
import Control.*;
import Entity.Activity_from_server;
import Entity.Extention_Stage_req;
import Entity.User;



public class ScreenManager extends JFrame {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final public static int DEFAULT_PORT = 2222;
	/** The only instance of the client (singleton) */
	private static ClientConsole client; 
	/** holds a flag for connection */
	public static boolean isFirstTime = true;
	/** Holds the server IP address */	
	//private static String serverIP = new String();
	/** Holds the port */
//	private static int port; 
	/** Holds the privilege level */
//	private int Plevel;
	private static User userName;
	private IPControl ip;
	private IP ip_gui;
	private Login login_gui;
	private Home homeScreen;
	private LoginControl login_control;
	private Main_screen mainScreen_gui;
	private ScreenManager j;
	private AddRequestControl add_req;
	private AddRequest add_req_gui;
	private EditRequest_GUI editReqGui;
	private DetermineStageManager detSM;
	private DetermineStageManger_GUI detSM_Gui;
	private requestFreeze_control freeze_control;
	private requestFreeze_GUI freeze_gui;
	private DetermineDuration stageDur;
	private DetermineStageDuration_GUI stageDur_Gui; 
	private ClosuerReq_control closuerS_control;
	private ClosureStage_GUI closuerS_gui;
	private EditReqStageMangerControl editReqSM;
	private EditRequestStageManager_GUI editReqSM_Gui;
	private EstimationStage_GUI stage1;
	private EstimationStageControl stage1_control;
	private ExaminationStage_GUI stage2;
	private ExaminationStageControl stage2_control;
	private performanceStage_GUI stage3;
	private PerformanceStageControl stage3_control;
	private TestingStage_GUI stage4;
	private TestingStageControl stage4_control;
	private OutPutConrol outPut;
	private OutPutRequest_GUI outPut_GUI;

	private Report_Control rep_control;
	private Report_GUI rep_Gui;

	private EstimationOutPut_GUI stage1_out_put_gui;
	private EstimationOutPutControl stage1_out_put;
	private viewRequest_control viewReq_control;
	private ViewRequest_GUI viewReq_gui; 
	private ExtensionRequest_GUI extend_req_Gui;
	private ExtentionStage extend_stage;
	private ViewReq_output_GUI viewReq_output;
	private UpdateAuthorization_GUI auth_GUI;
	private AuthorizationControl auth_control;
	private ExtensionRequestSuperV_Control ex_supervisor_control;
	private ExtentionReqManger_GUI supervisor_ex_first_gui;
	private ViewExtentionReq_GUI supervisor_ex_second_gui;
	private  DurationStageManager_GUI durationSM_gui;
	private DurationStageManager_Control durationSM_control;
    private ActivityReportOutput_Gui activRep_output_gui;
    private ActivityRepChart_GUI arc_gui;
    private TardinessReport_GUI tardinRep_output_gui;

    private FreezesManagement_GUI unfreeze_Gui;
    private Unfreeze_Control unfreeze_control;
    

    private ParformanceRep_GUI parfRep_gui;


	public ScreenManager(){
	
	   pack();
	   setMinimumSize(new Dimension(570, 700));
		setPreferredSize(new DimensionUIResource(570, 700));
		
		userName = new User();
		ip= new IPControl();
		homeScreen = new Home();
	    login_control = new LoginControl();
	    login_gui = login_control.getlogInPanel();
		ip_gui = ip.getIPPanel();
		mainScreen_gui = new Main_screen();
		add_req =new AddRequestControl();
		add_req_gui=add_req.getAdd_req_gui();
		editReqGui = new EditRequest_GUI();
		detSM = new DetermineStageManager();
		detSM_Gui = detSM.getDSMgui();
		freeze_control = new requestFreeze_control();
		freeze_gui = freeze_control.getFreezeReq_GUI();
		stageDur = new DetermineDuration();
		stageDur_Gui = stageDur.getDetDurGui();
		editReqSM = new EditReqStageMangerControl();
		editReqSM_Gui = editReqSM.getEditMangerGui();
		stage1_control= new EstimationStageControl();
		stage1 = stage1_control.getEstimationGui();
		stage2_control= new ExaminationStageControl();
		stage2 = stage2_control.getExaminationGui();
		stage3_control= new PerformanceStageControl();
		stage3 = stage3_control.getPerformanceGui();
		stage4_control= new TestingStageControl();
		stage4 = stage4_control.getTextingGui();
		outPut = new OutPutConrol();
		outPut_GUI = outPut.getOutPutReq_Gui();
		rep_control = new Report_Control();
		rep_Gui = rep_control.getRep_gui();
		stage1_out_put= new EstimationOutPutControl();
		stage1_out_put_gui=stage1_out_put.getStage1_output();
		ex_supervisor_control = new ExtensionRequestSuperV_Control();
		supervisor_ex_first_gui = ex_supervisor_control.getfirst_gui();
		supervisor_ex_second_gui = ex_supervisor_control.getsecond_gui();
		viewReq_control = new viewRequest_control();
		viewReq_gui = viewReq_control.getView_req_gui();
		extend_stage = new ExtentionStage();
		extend_req_Gui = extend_stage.getExt_stage_gui();
	   durationSM_control= new DurationStageManager_Control();
	   durationSM_gui= durationSM_control.getdurationSM_GUI();
	   
		// Home screen
	   tardinRep_output_gui = rep_control.getTRG_output();
	   activRep_output_gui = rep_control.getG_ARoutput();
	 // arc_gui = rep_control.getActivGraph_gui();
	   rep_Gui = rep_control.getRep_gui();
	   auth_control= new  AuthorizationControl();
	   auth_GUI= auth_control.getAuthGui();

	   unfreeze_control=new Unfreeze_Control();
	   unfreeze_Gui=unfreeze_control.getF_gui();

	   parfRep_gui = rep_control.getPR_output();

	 

		
	    add(homeScreen);
		setContentPane(homeScreen);
	    setVisible(true);
	    
	    
	    
	Action_listener_operations();
	}
	

	public static void main(String[] args){
	
	new ScreenManager();
	
	
	
		
	}


	public void  Action_listener_operations(){
		
		
		login_gui.getBtnCancel().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ip_gui.getTextField().setText("");
				//login_gui.getTextArea().setText("");
				//login_gui.getPasswordField().setText("");
				add(ip_gui);
				setContentPane(ip_gui);
			    setVisible(true);	
			}
		});
		
		login_gui.getBtnSubmit().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean ans = login_control.loginReq_pressed();
				if(ans){
					for (Entry<Integer, ArrayList<Integer>> entry : userName.getReq_auto_list().entrySet()) {
					    System.out.println(entry.getKey() + ", " + entry.getValue());
					}
					/*for(int i=0;i<getUserName().getReq_list().size();i++)			
						System.out.println(getUserName().getReq_list().get(i));*/
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
			    setVisible(true);}
				else{
					login_gui.getTextArea().setText("");
					login_gui.getPasswordField().setText("");
				}		
			}
		});
		
		homeScreen.getBtnNewButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				add(ip_gui);
				setContentPane(ip_gui);
			    setVisible(true);
				
			}
		});
		
		ip_gui.getBtnSubmit().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = ip.ip_entered();
				if(s.equals("")){
					JOptionPane.showMessageDialog(ip_gui, "You have to enter ip number ", "Error", JOptionPane.ERROR_MESSAGE);				
				}
				else{
					boolean ans = ip.makeConnection(s);
					if(ans){
					add(login_gui);
					setContentPane(login_gui);
					setVisible(true);
					}
					else{
						JOptionPane.showMessageDialog(ip_gui, "No conection was established", "Error", JOptionPane.ERROR_MESSAGE);	
						add(homeScreen);
						setContentPane(homeScreen);
						setVisible(true);
					}
				}}
			});
		
	/**********************************closurestage!!!**************************/	
		/** 
	 	 * when user presses on close stage button 
	 	* open  anew window white all the request to close 
	 	 */ 
 	editReqGui.getBtnCloseStage().addActionListener(new ActionListener() { 
	 		public void actionPerformed(ActionEvent e) { 
				 ArrayList<Integer> closeSarray = closuerS_control.getRequestToClose(); 
		 			 Iterator<Integer> d =closeSarray.iterator(); 
				 while(d.hasNext()) 
					 { 
		 				 Integer req_num = d.next(); 
		                  closuerS_gui.getReqNum().addItem(req_num); 
	               } 
	 			add(closuerS_gui); 
		 			 setContentPane(closuerS_gui); 
	 			 setVisible(true); 
		 		} 
	 	}); 
		 /** 
	  	 * press cancel on the closure stage panel 
	     * go back to to the edit request window 
	      */ 
		 
	 	closuerS_gui.getBtnCancel().addActionListener(new ActionListener() { 
		
		 		public void actionPerformed(ActionEvent e) { 

		 			add(editReqGui); 
                  setContentPane(editReqGui); 

		 			 setVisible(true); }});
	 	
	 	closuerS_gui.getBtnSubmit().addActionListener(new ActionListener() { 
		 		public void actionPerformed(ActionEvent e) { 

	 		 JOptionPane.showMessageDialog(null, "The request closed successfully ", "", JOptionPane.INFORMATION_MESSAGE); 
		 		}});
	 /****************************closure!!!!!***********************/
	 	
	 		 ip_gui.getCancelButton_1().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			ip_gui.getTextField().setText("");
			add(homeScreen);
			setContentPane(homeScreen);
			setVisible(true);
		}
	});//****unfreeze request*****/////////
		mainScreen_gui.getBtnFrozenMangment().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(userName.getRole()!=1){
					JOptionPane.showMessageDialog(	null, "You are not authorize to do this action!", "Error", JOptionPane.ERROR_MESSAGE);	
					add(mainScreen_gui);
					setContentPane(mainScreen_gui);
					setVisible(true);
				}
				else{
				ArrayList<Integer> i=unfreeze_control.show_gui_screen();
				Iterator<Integer> it=i.iterator();
				unfreeze_Gui.getComboBox_choose_req().removeAllItems();
				while(it.hasNext()){
					Integer l=it.next();
					unfreeze_Gui.getComboBox_choose_req().addItem(l);	
				}
				
				add(unfreeze_Gui);
				setContentPane(unfreeze_Gui);
				setVisible(true);
				}}});
		unfreeze_Gui.getBtnFreeze().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				unfreeze_control.unfreeze_pressed((Integer) unfreeze_Gui.getComboBox_choose_req().getSelectedItem());
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
	
			}});
		unfreeze_Gui.getCancel_button().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			
			}});
		

		
		
	
	mainScreen_gui.getBtnAddRequest().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    add(add_req_gui);
			setContentPane(add_req_gui);
			setVisible(true);	
		}
	});
	
	mainScreen_gui. getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			login_control.log_out_pressed(userName.getUserName());
			login_gui.getTextArea().setText("");
			login_gui.getPasswordField().setText("");
		    add(login_gui);
			setContentPane(login_gui);
			setVisible(true);
		}
		
	});
	
	mainScreen_gui.getBtnReports().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(rep_Gui);
			setContentPane(rep_Gui);
			setVisible(true);
			
		}
	});
	
	
	add_req_gui.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			boolean ans = add_req.AddRequest_pressed();
			if(ans){
				add_req_gui.getInfoSystem().setSelectedIndex(0);
				add_req_gui.getCurStatus().setText("");
				add_req_gui.getChaDes().setText("");
				add_req_gui.getArgu().setText("");
				add_req_gui.getComm().setText("");
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
			    setVisible(true);	
			}
			else{
				add(add_req_gui);
				setContentPane(add_req_gui);
			    setVisible(true);
			}
			
			
		}
	});
	
	add_req_gui.getCanButton().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			add_req_gui.getInfoSystem().setSelectedIndex(0);
				add_req_gui.getCurStatus().setText("");
				add_req_gui.getChaDes().setText("");
				add_req_gui.getArgu().setText("");
				add_req_gui.getComm().setText("");
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
			    setVisible(true);	
			}
		});
	
	mainScreen_gui.getBtnEditRequest().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(userName.getRole()==2){
				add(editReqGui);
				setContentPane(editReqGui);
				setVisible(true);	}
			else if(userName.getRole()==8){
					if(userName.getReq_auto_list().isEmpty())
						JOptionPane.showMessageDialog(ip_gui, "YOU HAVE NO RELATED REQUESTS", "Error", JOptionPane.ERROR_MESSAGE);		
					else{
						add(editReqSM_Gui);
						setContentPane(editReqSM_Gui);
						editReqSM_Gui.getReqNum().removeAllItems();
						editReqSM_Gui.getReqNum().addItem("");
						for (Entry<Integer, ArrayList<Integer>> entry : userName.getReq_auto_list().entrySet()) {
							Iterator<Integer> it = entry.getValue().iterator();
							while(it.hasNext())		
								editReqSM_Gui.getReqNum().addItem(it.next());
						}
						setVisible(true);
					}
			}else if(userName.getRole()==3 || userName.getRole()==4){
				add(editReqSM_Gui);
				setContentPane(editReqSM_Gui);
				Iterator<String> it = userName.getReq_list().iterator();
				editReqSM_Gui.getReqNum().removeAllItems();
				while(it.hasNext())		
					editReqSM_Gui.getReqNum().addItem(it.next());
				setVisible(true);
			}else
				JOptionPane.showMessageDialog(null, "You have no authorization", "ERROR", JOptionPane.ERROR_MESSAGE);
			
		}
	});
	/******************************ViewReq**************************************/
	mainScreen_gui.getBtnViewRequest().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			 ArrayList<String> view = viewReq_control.get_requests();
			 Iterator<String> c =view.iterator();
			 while(c.hasNext()){
				 String req_num = c.next();
				 viewReq_gui.getChoose_req_comboBox().addItem(req_num);}			
			add(viewReq_gui);
			setContentPane(viewReq_gui);
			setVisible(true);
		}
});
	 	viewReq_gui.getBtnSubmit().addActionListener(new ActionListener() { 
		public void actionPerformed(ActionEvent e) { 
	 	 String [] s =viewReq_control.view_request((String) viewReq_gui.getChoose_req_comboBox().getSelectedItem()); 
	 viewReq_output.getRequest_num_textField().setText(viewReq_gui.getChoose_req_comboBox().getSelectedItem().toString()); 
	 viewReq_output.getCurrent_stage_textField().setText(s[1]); 
	 	 viewReq_output.getExpexted_date_textField().setText(s[2]); 
	 viewReq_output.getStatus_textField().setText(s[0]); 
	 add(viewReq_output); 
		setContentPane(viewReq_output); 
	setVisible(true); 
	}}); 
	 	viewReq_output.getBtnBack().addActionListener(new ActionListener() { 
 		public void actionPerformed(ActionEvent e) { 
 			add(mainScreen_gui); 
	 			setContentPane(mainScreen_gui); 
				setVisible(true); }});
				 

	 
	viewReq_gui.getCancel_button().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(mainScreen_gui);
			setContentPane(mainScreen_gui);
			setVisible(true);
		}
	});
	
/************************************viewReq*********************************/
	editReqGui.getBtnDetermineStageManger().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(detSM_Gui);
			setContentPane(detSM_Gui);
		
			
			ArrayList<String> list =detSM.getWaitingReq();
			if(list.isEmpty())
				JOptionPane.showMessageDialog(ip_gui, "There are no new requests", "Error", JOptionPane.ERROR_MESSAGE);		
			else{
				 Iterator<String> it = list.iterator();
				 detSM_Gui.getComboBox_req_num().removeAll();
				 detSM_Gui.getComboBox_req_num().addItem("");
				 while(it.hasNext())		
					 detSM_Gui.getComboBox_req_num().addItem(it.next());
				 setVisible(true);
			}
			detSM.getInformationEngineer();
			detSM_Gui.getComboBox_req_num().addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					detSM_Gui.getComboBox_performance().removeAll();
					detSM_Gui.getComboBox_examination_stage().removeAll();
					detSM_Gui.getComboBox_estimation_stage().removeAll();
					detSM_Gui.getComboBox_performance().addItem("");
					String x = (String) detSM_Gui.getComboBox_req_num().getSelectedItem();
					String estimatorID = detSM.getEstimator(Integer.parseInt(x)).toString();
					detSM_Gui.getComboBox_estimation_stage().addItem(detSM.getEstimator(Integer.parseInt(x)));
					detSM_Gui.getComboBox_examination_stage().addItem(detSM.getChairman());
					
					java.util.Iterator<String> it2 = detSM.getInfoEngineer().iterator();
					while(it2.hasNext())
							detSM_Gui.getComboBox_performance().addItem(it2.next());
					
					java.util.Iterator<String> it1 = detSM.getInfoEngineer().iterator();
					while(it1.hasNext()){
						String s = it1.next();
						if(!(s.equals(estimatorID)))
							detSM_Gui.getComboBox_estimation_stage().addItem(s);
					}
					Iterator<Integer> it4 = detSM.getCommittee().iterator();
					detSM_Gui.getComboBox_testing_stage().addItem("");
						while(it4.hasNext())		
							detSM_Gui.getComboBox_testing_stage().addItem(it4.next());
				}
			});
				
			setVisible(true);	

		}
	});
	
	editReqGui.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(mainScreen_gui);
			setContentPane(mainScreen_gui);
		    setVisible(true);
		}
	});
	
	detSM_Gui.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		 boolean ans= detSM.submitBnt_prassed();
		 if(!ans){
			add(detSM_Gui);
			setContentPane(detSM_Gui);
			setVisible(true);
		 }
		 else{
			 add(mainScreen_gui);
			 setContentPane(mainScreen_gui);
			 setVisible(true);
		 }
		}
	});
	
	//Cancel button was pressed
	detSM_Gui.getButton().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(editReqGui);
			setContentPane(editReqGui);
			setVisible(true);
		}
	});
	
	
	editReqGui.getBtnFrozenRequest().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			 ArrayList<Integer> ac=freeze_control.getActive();
			 Iterator<Integer> a=ac.iterator();
			 while(a.hasNext()){
				 Integer req_num=a.next();
				 freeze_gui.getReqNum().addItem(req_num);}
			 add(freeze_gui);
			 setContentPane(freeze_gui);
			 setVisible(true);	   }
	});
	
	editReqGui.getBtnDetermineStageDuration().addActionListener(new ActionListener(){ 
		public void actionPerformed(ActionEvent e) {
			add(stageDur_Gui);
			setContentPane(stageDur_Gui);
			ArrayList<String> list =detSM.getWaitingReq();
			if(list.isEmpty())
				JOptionPane.showMessageDialog(ip_gui, "There are no new requsts", "Error", JOptionPane.ERROR_MESSAGE);		
			else{
				 Iterator<String> it = list.iterator();
				 stageDur_Gui.getReqNum().addItem("");
				 while(it.hasNext())		
					 stageDur_Gui.getReqNum().addItem(it.next());
				 setVisible(true);
			}
			stageDur_Gui.getReqNum().addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Integer reqNum = Integer.parseInt((String) stageDur_Gui.getReqNum().getSelectedItem());
					stageDur.getCurrDur(reqNum);
					stageDur_Gui.getStage1Dur().setText(Integer.toString(stageDur.getCurrDur()[0]));
					stageDur_Gui.getStage3Dur().setText(Integer.toString(stageDur.getCurrDur()[1]));
					
				}
			});
			setVisible(true);
					
			
		}
	});
	
	
	/**
	 * submit button in determine stage duration was pressed if the stage duration added successfully we will go bake to the main window
	 */
	stageDur_Gui.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			 boolean ans = stageDur.submit_pressed();
			 if(ans){
				 add(mainScreen_gui);
				 setContentPane(mainScreen_gui);
				 setVisible(true);
			 }	
		}
	});

	/**
	 * cancel button in determine stage duration was pressed , we go bake to edit request window
	 */
	stageDur_Gui.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(editReqGui);
			setContentPane(editReqGui);
			setVisible(true);
		}	
	});

	
	freeze_gui.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(editReqGui);
			setContentPane(editReqGui);
			setVisible(true);
			
		}
	});
	
	freeze_gui.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			Integer x= (Integer)freeze_gui.getReqNum().getSelectedItem();
			freeze_control.freeze_req(x);
			freeze_gui.getReqNum().setSelectedIndex(0);
			add(mainScreen_gui);
			setContentPane(mainScreen_gui);
			setVisible(true);
		}
	});
	

	
	
	
	editReqSM_Gui.getBtnUpdate().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			int ans = editReqSM.checkPer();
			if(ans==5){
				add(stage1);
				setContentPane(stage1);
				setVisible(true);
			}
			else if(ans==6){
				add(stage2);
				setContentPane(stage2);
				setVisible(true);
			} 
			else if(ans==7){
				add(stage3);
				setContentPane(stage3);
				setVisible(true);
			} 
			else if(ans==9){
				add(stage4);
				setContentPane(stage4);
				setVisible(true);
			} 
			else{
				JOptionPane.showMessageDialog(null, "you are not authorized", "Error", JOptionPane.ERROR_MESSAGE);		
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			}
		}
	});
	editReqSM_Gui.getBtnExtension().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			int ans = editReqSM.checkPer();
			if(ans!=0){
				add(extend_req_Gui);
				setContentPane(extend_req_Gui);
				setVisible(true);
			}
			else {
				JOptionPane.showMessageDialog(ip_gui, "you are not authorized", "Error", JOptionPane.ERROR_MESSAGE);		
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			}
		}
	});
	/**
	 * this button submit the stage manager's extended stage request 
	 */
	extend_req_Gui.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			extend_stage.extSubb_pressed(Integer.parseInt(editReqSM_Gui.getReqNum().getSelectedItem().toString()),editReqSM_Gui.getStagename().getSelectedItem().toString());
			add(mainScreen_gui);
			setContentPane(mainScreen_gui);
			setVisible(true);
		}
	});
	/**
	 * this button open a window with all request details
	 */
	stage1.getBtnViewRequest().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			outPut.setReq(Integer.parseInt(editReqSM.getReqANDstage()[0]));
			add(outPut_GUI);
			setContentPane(outPut_GUI);
			setVisible(true);	
			}
	});
	stage1.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			boolean ans = stage1_control.estimation_Submit_pressed(Integer.parseInt(editReqSM_Gui.getReqNum().getSelectedItem().toString()));
			if(ans){
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			}
				
		}
	});
	/**
	 * if cancel button was pressed go bake to estimation window
	 */
	outPut_GUI.getCanButton().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(stage1);
			setContentPane(stage1);
			setVisible(true);
			
		}
	});
	rep_Gui.getBtnPefarmanceReport().addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			boolean x = rep_control.parformance_pressed();
			add(parfRep_gui);
			 setContentPane(parfRep_gui);
			 setVisible(true);
		}
	});
		

	    rep_Gui.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			add(mainScreen_gui);
			 setContentPane(mainScreen_gui);
			 setVisible(true);
		}
	});
	    rep_Gui.getBtnActivityReport().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		boolean x =	rep_control.ActivityReport_pressed();
		if(x){
			add(activRep_output_gui);
			 setContentPane(activRep_output_gui);
			 setVisible(true);
		}
		
		}
})	;
    rep_Gui.getBtnTardinessReport().addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		boolean x =	rep_control.TardinessReport_pressed();
		if(x){
			add(tardinRep_output_gui);
			 setContentPane(tardinRep_output_gui);
			 setVisible(true);
		}
		else{
			JOptionPane.showMessageDialog(null, "End Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);

		}

		}
	});
    tardinRep_output_gui.getBtnback().addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		rep_Gui.getStartDay_textField().setText("");
		rep_Gui.getEndDate_textField_1().setText("");
		add(rep_Gui);
		 setContentPane(rep_Gui);
		 setVisible(true);
		
	}
});
    activRep_output_gui.getBtnBack().addActionListener(new ActionListener() {
	
		public void actionPerformed(ActionEvent e) {
			rep_Gui.getStartDay_textField().setText("");
			rep_Gui.getEndDate_textField_1().setText("");
			add(rep_Gui);
			 setContentPane(rep_Gui);
			 setVisible(true);
			
		}
	});

	activRep_output_gui.getBtnPressToView().addActionListener(new ActionListener() {
		
		
		public void actionPerformed(ActionEvent e) {
		Activity_from_server ac=rep_control.graph_was_pressed();
			
					
			ActivityRepChart_GUI demo = new ActivityRepChart_GUI(ac.getActive());
	        demo.pack();
	        RefineryUtilities.centerFrameOnScreen(demo);
	        demo.setVisible(true);
			
		}
	});
	
activRep_output_gui.getBtnPressToViewFrozen().addActionListener(new ActionListener() {
		
		
		public void actionPerformed(ActionEvent e) {
		Activity_from_server ac=rep_control.graph_was_pressed();
			
					
			ActivityRepChart_GUI demo = new ActivityRepChart_GUI(ac.getFrozen());
	        demo.pack();
	        RefineryUtilities.centerFrameOnScreen(demo);
	        demo.setVisible(true);
		}
		});

activRep_output_gui.getBtnPressToViewDenied().addActionListener(new ActionListener() {
	
	
	public void actionPerformed(ActionEvent e) {
	Activity_from_server ac=rep_control.graph_was_pressed();
		
				
		ActivityRepChart_GUI demo = new ActivityRepChart_GUI(ac.getRejected());
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
	}
	});
activRep_output_gui.getBtnPressToViewclosed().addActionListener(new ActionListener() {
	
	
	public void actionPerformed(ActionEvent e) {
	Activity_from_server ac=rep_control.graph_was_pressed();
		
				
		ActivityRepChart_GUI demo = new ActivityRepChart_GUI(ac.getClosed());
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
	}
	});

parfRep_gui.getBtnBack().addActionListener(new ActionListener() {
	
	
	public void actionPerformed(ActionEvent e) {
		rep_Gui.getStartDay_textField().setText("");
		rep_Gui.getEndDate_textField_1().setText("");
		add(rep_Gui);
		 setContentPane(rep_Gui);
		 setVisible(true);
		
	}
});

	
/**
 * extension request button for supervisor-when button is pressed the extension request window will appear
 */
editReqGui.getBtnExtentionRequestManger().addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		 ArrayList<Extention_Stage_req> ex_r = ex_supervisor_control.getExReq();
		 Iterator<Extention_Stage_req> b= ex_r.iterator();
		 supervisor_ex_first_gui.getChooseReq_comboBox().removeAllItems();
		 while(b.hasNext()){
			 Integer req_num=b.next().getR_id();
			 supervisor_ex_first_gui.getChooseReq_comboBox().addItem(req_num);}
		 add(supervisor_ex_first_gui);
		 setContentPane(supervisor_ex_first_gui);
		 setVisible(true);	   }
});
/**
 * 
 *when pressing the cancel button the edit request window will appear
 */
supervisor_ex_first_gui.getCancelbutton().addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		add(editReqGui);
		 setContentPane(editReqGui);
		 setVisible(true);
	}
});

supervisor_ex_first_gui.getBtnSubmit().addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		String s = supervisor_ex_first_gui.getChooseReq_comboBox().getSelectedItem().toString();
	    if(s==null){
			JOptionPane.showMessageDialog(ip_gui, "No item was selected", "Error", JOptionPane.ERROR_MESSAGE);		
			 add(editReqGui);
			 setContentPane(editReqGui);
			 setVisible(true);
	    	
	    }
	    else {
	    Extention_Stage_req exten=ex_supervisor_control.extenSubmit_pressed(s);
	    supervisor_ex_second_gui.getExtentionrequestdatetextField().setText(exten.getTimestamp().toLocaleString());
	    supervisor_ex_second_gui.getRequestnumbertextField().setText(exten.getR_id().toString());
	    supervisor_ex_second_gui.getStagenametextField().setText(exten.getStageName());
	    supervisor_ex_second_gui.getNumberofdaystoextendtextField_3().setText(exten.getNumDay().toString());
	    supervisor_ex_second_gui.getArgumenttextArea().setText(exten.getArrgumant());
	    add(supervisor_ex_second_gui);
		 setContentPane(supervisor_ex_second_gui);
		 setVisible(true);
    	
	    }
	}
});
supervisor_ex_second_gui.getBtnSubmit().addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
			String req_num= supervisor_ex_second_gui.getRequestnumbertextField().getText(); 
    String stage_name= supervisor_ex_second_gui.getStagenametextField().getText(); 
    String num_days=supervisor_ex_second_gui.getNumberofdaystoextendtextField_3().getText(); 
	    ex_supervisor_control.extend_request(req_num,stage_name,num_days); 
  add(mainScreen_gui); 
	 setContentPane(mainScreen_gui); 
 		 setVisible(true); 
	
	 	} 
	 
	 }); 

	 supervisor_ex_second_gui.getBtnReject().addActionListener(new ActionListener() { 
	 	public void actionPerformed(ActionEvent e) { 

	 		String req_num= supervisor_ex_second_gui.getRequestnumbertextField().getText(); 
	  String stage_name= supervisor_ex_second_gui.getStagenametextField().getText(); 
	    String num_days=supervisor_ex_second_gui.getNumberofdaystoextendtextField_3().getText(); 
	 ex_supervisor_control.reject_request(req_num, stage_name, num_days); 
		 add(mainScreen_gui); 
	  setContentPane(mainScreen_gui); 
		 setVisible(true); 
	 	}}); 

	
	
	stage2.getBtnViewEstimationReport().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			stage1_out_put.setReq(Integer.parseInt(editReqSM.getReqANDstage()[0]));
			add(stage1_out_put_gui);
			setContentPane(stage1_out_put_gui);
			setVisible(true);
			
		}
	});
	
	stage2.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			boolean ans=stage2_control.examination_Submit_pressed(Integer.parseInt(editReqSM_Gui.getReqNum().getSelectedItem().toString()));
			if(!ans){
				stage2.getDecision().setSelectedIndex(0);
				add(stage2);
				setContentPane(stage2);
				setVisible(true);
			}
			else{
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			}
		}
	});
	
	stage2.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(editReqSM_Gui);
			setContentPane(editReqSM_Gui);
			setVisible(true);
		}
	});
/**
 * this button will show the estimation report to the performance leader
 */
	stage3.getBtnViewEstimationReport().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			stage1_out_put.setReq(Integer.parseInt(editReqSM.getReqANDstage()[0]));
			add(stage1_out_put_gui);
			setContentPane(stage1_out_put_gui);
			setVisible(true);
			
		}
	});
	

	/**
	 * this button go bake to the previous window, to the examination's window or preformance's window
	 */
	stage1_out_put_gui.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(userName.getRole()==6){
				add(stage2);
				setContentPane(stage2);
				setVisible(true);
			}else if(userName.getRole()==7){
				add(stage3);
				setContentPane(stage3);
				setVisible(true);
			}else{
				add(stage4);
				setContentPane(stage4);
				setVisible(true);
			}
		}
	});
	/**
	 * this button present the performance window 
	 */
	stage3.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			boolean ans=stage3_control.performance_Submit_pressed((Integer.parseInt(editReqSM_Gui.getReqNum().getSelectedItem().toString())));
			if(ans){
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			}
		}
	});
	
	editReqSM_Gui.getBtnDuration().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			int auth=editReqSM.checkPer();
			if(auth!=0){
				add(durationSM_gui);
				setContentPane(durationSM_gui);
				setVisible(true);
			}
			else{
				JOptionPane.showMessageDialog(null, "you are not authorized", "Error", JOptionPane.ERROR_MESSAGE);	
			}
		}
	});

	/**
	 * this button cancels the current operation and go bake to the previous window
	 */
	stage3.getButton().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(editReqSM_Gui);
			setContentPane(editReqSM_Gui);
			setVisible(true);
		}
	});
	/**
	 * this button will show the estimation report to the tester
	 */
		stage4.getBtnViewEstimationReport().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				stage1_out_put.setReq(Integer.parseInt(editReqSM.getReqANDstage()[0]));
				add(stage1_out_put_gui);
				setContentPane(stage1_out_put_gui);
				setVisible(true);
				
			}
		});
		/**
		 * this button present the tester window 
		 */
	
		stage4.getBtnSubmit().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean ans=stage4_control.testing_Submit_pressed((Integer.parseInt(editReqSM_Gui.getReqNum().getSelectedItem().toString())));
				if(ans){
					add(mainScreen_gui);
					setContentPane(mainScreen_gui);
					setVisible(true);
				}
				else{
					stage4.getDecision().removeAll();
					stage4.getComments().setText("");	
				}
			}
		});
		
		/**
		 * this button cancels the current operation and go bake to the previous window
		 */
		stage4.getCancel_button().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				add(editReqSM_Gui);
				setContentPane(editReqSM_Gui);
				setVisible(true);
			}
		});

	durationSM_gui.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(editReqSM_Gui);
			setContentPane(editReqSM_Gui);
			setVisible(true);
			
		}
	});
	
/*	durationSM_gui.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			boolean x= durationSM_control.submit_pressed(editReqSM_Gui.getReqNum().getSelectedItem().toString(),editReqSM_Gui.getReqNum().getSelectedItem().toString());
			if(!x){
				durationSM_gui.getNumDay().setSelectedIndex(0);
			}
			else{
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			}
		}
	});*/
	

	mainScreen_gui.getBtnAuthorizations().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(userName.getRole()==1){
				auth_GUI.getAuthorization().setSelectedIndex(0);
				auth_GUI.getUser_textField().setText("");
				add(auth_GUI);
				setContentPane(auth_GUI);
				setVisible(true);
				}
		else{
			JOptionPane.showMessageDialog(null, "you are not authorized", "Error", JOptionPane.ERROR_MESSAGE);	
		}}
	});
	
	auth_GUI.getCancelbutton().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			auth_GUI.getAuthorization().setSelectedIndex(0);
			auth_GUI.getUser_textField().setText("");
			add(mainScreen_gui);
			setContentPane(mainScreen_gui);
			setVisible(true);
		}
	});
	
	auth_GUI.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			boolean ans=auth_control.updateAuthorization_pressed();
			if (!ans){
				auth_GUI.getAuthorization().setSelectedIndex(0);
				auth_GUI.getUser_textField().setText("");
				
			}
			else{
				add(mainScreen_gui);
				setContentPane(mainScreen_gui);
				setVisible(true);
			}
		}
	});
	
	extend_req_Gui.getBtnSubmit().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			extend_stage.extSubb_pressed(Integer.parseInt(editReqSM_Gui.getReqNum().getSelectedItem().toString()),( editReqSM_Gui.getStagename().getSelectedItem().toString()));
			add(mainScreen_gui);
			setContentPane(mainScreen_gui);
			setVisible(true);
		}
	});
	
	extend_req_Gui.getBtnCancel().addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(editReqSM_Gui);
			setContentPane(editReqSM_Gui);
			setVisible(true);
		}
	});
	
	}// end function
		
		
		public static ClientConsole getConnection(String s, int port)

		{
			if(client== null){
				client = new ClientConsole(s, port);
			}
			return client;
			
		}
	    	
		/**
		* Get the only singleton connection
		* This method is read without parameters
		* so the connection is the same as before
		*/
		public static ClientConsole getConnection() {
			return client;
		}
		
		public static User getUserName(){
			return userName;
		}
	    	
	}
		
		